package org.daidai.abstrusewriting.homepage.live.adapters;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.live.models.AlbumIdListItem;

import java.util.IllegalFormatCodePointException;
import java.util.List;

/**
 * Created by Administrator on 2016/11/2.
 */

public class MyAlbumIdAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private List<AlbumIdListItem> listItems;

    public MyAlbumIdAdapter(Context context) {
        this.context = context;

    }
    public void set(List<AlbumIdListItem> l)   {
        listItems=l;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (listItems != null) {
            ret = listItems.size();
        }
        //Log.d("777777777", "getCount: "+ret);
        return ret;
    }

    @Override
    public Object getItem(int i) {
        return listItems.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View ret = null;
        if (view==null)
        {
            ret= LayoutInflater.from(context).inflate(R.layout.live_sound_item,viewGroup,false);
        }
        else
        {
            ret=view;
        }
        LiveViewHolder live = (LiveViewHolder) ret.getTag();
        if (live==null)
        {
            live =new LiveViewHolder(ret);
            ret.setTag(live);
        }
        Picasso.with(live.live_sound_iv.getContext())
                .load(listItems.get(i).getSmallLogo())
                .config(Bitmap.Config.RGB_565)
                .into(live.live_sound_iv);
        live.live_sound_tv.setText(listItems.get(i).getTitle());
        live.live_sound_num.setText(listItems.get(i).getPlaytimes());
        live.live_sound_dian.setText(listItems.get(i).getLikes()+"");
        live.abuimd_download.setOnClickListener(this);
        return ret;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.abuimd_download:
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("下载")
                        .setMessage("你确定要下载吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(context, "此视频不支持下载", Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
                break;
        }
    }

    private static class  LiveViewHolder
    {
        private ImageView live_sound_iv;
        private TextView live_sound_tv;
        private TextView live_sound_num;
        private TextView live_sound_dian;
        private ImageView abuimd_download;
        public LiveViewHolder(View itemView)
        {
            live_sound_iv= (ImageView) itemView.findViewById(R.id.live_sound_iv);
            live_sound_tv = (TextView) itemView.findViewById(R.id.live_sound_tv);
            live_sound_num = (TextView) itemView.findViewById(R.id.live_sound_num);
            live_sound_dian = (TextView) itemView.findViewById(R.id.live_sound_dian);
            abuimd_download = (ImageView) itemView.findViewById(R.id.abuimd_download);
        }
    }
}
